package com.suneee.store.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.suneee.base.util.ContextHelper;
import com.suneee.core.error.Assert;
import com.suneee.core.error.ErrorUtils;
import com.suneee.resources.ProductError;
import com.suneee.spec.dao.SpecDao;
import com.suneee.spec.entity.Spec;
import com.suneee.spec.service.SpecService;
import com.suneee.store.bean.EnterpriseCheckResult;
import com.suneee.store.dao.EnterpriseSpecDao;
import com.suneee.store.dao.SkuDao;
import com.suneee.store.dao.SpuDao;
import com.suneee.store.entity.EnterpriseSpec;
import com.suneee.store.entity.Sku;
import com.suneee.store.entity.Spu;
import com.suneee.store.service.EnterpriseSpecService;

@Service("api-enterpriseSpec")
public class EnterpriseSpecServiceImpl implements EnterpriseSpecService {

	@Autowired
	private EnterpriseSpecDao enterpriseSpecDao;
	
	@Autowired
	private SpecService specService;
	
	@Autowired
	private SpuDao spuDao;
	
	@Autowired
	private SkuDao skuDao;
	
	@Autowired
	private SpecDao specDao;
	
	@Override
	@Transactional
	public void addEnterpriseSpec(List<EnterpriseSpec> enterpriseSpecList,List<Integer> deletes) {
		List<EnterpriseSpec> updates = new ArrayList<>();
		List<EnterpriseSpec> inserts = new ArrayList<>();
		for(EnterpriseSpec s : enterpriseSpecList){
			s.setEnterpriseCode(ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode());

			if(s.getId() != null)
				updates.add(s);
			else
				inserts.add(s);
		}

		if(deletes != null && !deletes.isEmpty())
			enterpriseSpecDao.delete(deletes);
		if(!updates.isEmpty())
			enterpriseSpecDao.update(updates);
		if(!inserts.isEmpty()){
			for(EnterpriseSpec spec : inserts){
				EnterpriseSpec temp = enterpriseSpecDao.getEnterpriseSpecByName(
						ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode(), 
						spec.getSpecId(), spec.getSpecValue());
				if(temp != null)
					throw ErrorUtils.wrap(ProductError.ENTERPRISE_NO_ROLE,"规格值：" + spec.getSpecValue() + ",已存在，不能重复添加！");
			}
			enterpriseSpecDao.insert(inserts);
		}
	}

	@Override
	public List<Spec> listView(String categoryCode) {
		// TODO Auto-generated method stub
		List<Spec> specList = specService.getSpecByCategoryCode(categoryCode);
		for(Spec spec : specList){
			List<EnterpriseSpec> enterpriseSpecList = enterpriseSpecDao.queryEnterpriseSpecBySpecId(spec.getSpecId());
			spec.setEnterpriseSpecList(enterpriseSpecList);
		}
		return specList;
	}

	@Override
	public void deleteEnterpriseSpec(Integer id) {
		// TODO Auto-generated method stub
		EnterpriseSpec enterpriseSpec = enterpriseSpecDao.select(id);
		if(!enterpriseSpec.getEnterpriseCode().equals(ContextHelper.getCurrentLoginInfo()
				.getEnterprise().getEnterpriseCode()))
			throw ErrorUtils.wrap(ProductError.ENTERPRISE_NO_ROLE,"没有权限操作此对象！");
		enterpriseSpecDao.delete(id);
	}

	@Override
	public EnterpriseCheckResult checkIsUsed(Integer id) {
		// TODO Auto-generated method stub
		EnterpriseSpec enterpriseSpec = enterpriseSpecDao.select(id);
		if(!enterpriseSpec.getEnterpriseCode().equals(ContextHelper.getCurrentLoginInfo()
				.getEnterprise().getEnterpriseCode()))
			throw ErrorUtils.wrap(ProductError.ENTERPRISE_NO_ROLE,"没有权限操作此对象！");
		List<Spu> spuList = spuDao.getListBySpecIdAndEnterpriseCode(enterpriseSpec.getSpecId(),
				ContextHelper.getCurrentLoginInfo().getEnterprise().getEnterpriseCode());
		EnterpriseCheckResult result = new EnterpriseCheckResult();
		result.setId(enterpriseSpec.getId());
		result.setValue(enterpriseSpec.getSpecValue());
		if(spuList == null){
			result.setCheckedResult(false);
		}
		else{
			for(Spu spu : spuList){
				List<Sku> skuList = skuDao.getListBySpuIdAndSpecValue(spu.getSpuId(), enterpriseSpec.getSpecValue());
				if(skuList != null && !skuList.isEmpty()){
					result.setCheckedResult(true);
					List<Integer> skuIdList = new ArrayList<>();
					for(Sku sku : skuList){
						skuIdList.add(sku.getSkuId());
					}
					if(result.getSkuIdList() == null || result.getSkuIdList().isEmpty())
						result.setSkuIdList(skuIdList);
					else
						result.getSkuIdList().addAll(skuIdList);
				}
				else{
					if(result.getSkuIdList() == null || result.getSkuIdList().isEmpty())
						result.setCheckedResult(false);
				}
			}
		}
		return result;
	}

	@Override
	public List<Spec> getSpuSpecList(Integer spuId) {
		// TODO Auto-generated method stub
		Spu spu = spuDao.select(spuId);
		List<Spec> list = new ArrayList<>();
		if(spu.getSpecIdOne() != null){
			Spec spec = specDao.select(spu.getSpecIdOne());
			if(spec != null){
				List<EnterpriseSpec> enterpriseSpecList = enterpriseSpecDao.queryEnterpriseSpecBySpecId(spec.getSpecId());
				spec.setEnterpriseSpecList(enterpriseSpecList);
				list.add(spec);
			}
		}
		if(spu.getSpecIdTwo() != null){
			Spec spec = specDao.select(spu.getSpecIdTwo());
			if(spec != null){
				List<EnterpriseSpec> enterpriseSpecList = enterpriseSpecDao.queryEnterpriseSpecBySpecId(spec.getSpecId());
				spec.setEnterpriseSpecList(enterpriseSpecList);
				list.add(spec);
			}
		}
		if(spu.getSpecIdThree() != null){
			Spec spec = specDao.select(spu.getSpecIdThree());
			if(spec != null){
				List<EnterpriseSpec> enterpriseSpecList = enterpriseSpecDao.queryEnterpriseSpecBySpecId(spec.getSpecId());
				spec.setEnterpriseSpecList(enterpriseSpecList);
				list.add(spec);
			}
		}
		if(spu.getSpecIdFour() != null){
			Spec spec = specDao.select(spu.getSpecIdFour());
			if(spec != null){
				List<EnterpriseSpec> enterpriseSpecList = enterpriseSpecDao.queryEnterpriseSpecBySpecId(spec.getSpecId());
				spec.setEnterpriseSpecList(enterpriseSpecList);
				list.add(spec);
			}
		}
		return list;
	}

}
